/****************************************
* James Gomez                           *
* Project 3: Target Practice P3         *
* Comp 465                              *
* Professor Barnes                      *
* 12/7/2012                             *
*****************************************/

#ifndef _MISSILE_BATTERY_
#define _MISSILE_BATTERY_

#include "GomezJamesP3_OpenGL.h"

#include "GomezJamesP3_CollidableObject.cpp"
#include "GomezJamesP3_Movable.cpp"
#include "GomezJamesP3_Explodable.cpp"

#include "GomezJamesP3_Torpedo.cpp"
#include "GomezJamesP3_Missile.cpp"

#include <string>
using namespace std;

extern ::vector<Torpedo*> torpedos;

/********************************************************************
Missile batteries can fire homing photon torpedos at the Player Ship.
Missile batteries must be anchored onto a moon or planet.
*********************************************************************/
class MissileBattery: public CollidableObject, Movable, public Explodable{

private:
	Object3D * anchor;
	CollidableObject * target;

	Color color[3];
	GLint torpedoCount;
	Torpedo * currentTorpedo;
	GLfloat missileLifeSpan;
	GLfloat detectionRadius;	

public:
	MissileBattery(const string name, const Color color[3], Object3D*, CollidableObject*,
		const GLfloat missileLifeSpan, const GLfloat detectR, const GLfloat offset[3]);
	~MissileBattery(){}

	GLfloat getdetectionRadius(){ return detectionRadius; }

	void draw();
	void move();	

	void fireTorpedo();

};


MissileBattery::MissileBattery(const string name, const Color color[3], Object3D* anchor, CollidableObject* target,
							   const GLfloat missileLifeSpan, const GLfloat detectR, const GLfloat offset[3]): 
								CollidableObject(name, 75), Explodable(){
	
	this->anchor = anchor;
	this->target = target;
	this->missileLifeSpan = missileLifeSpan;
	detectionRadius = detectR;
	currentTorpedo = NULL;
	torpedoCount = 10;

	for (int i=0; i<3; i++)
		this->color[i] = color[i];		

	//save offset translation in OM
	glMatrixMode(GL_MODELVIEW);
	glPushMatrix();
	glLoadMatrixf(OM);
	glTranslatef(offset[0], offset[1], offset[2]);
	glGetFloatv(GL_MODELVIEW_MATRIX, OM);
	glPopMatrix();						
}

void MissileBattery::draw(){

	if (isActive){
		if(isExploding){
			drawExplosion(TM);
		}
		else{
			glMatrixMode(GL_MODELVIEW);
			glPushMatrix();
			glMultMatrixf(TM);
			//drawAxes(200);
			glColor3fv(color);
			glutSolidCube(50);
			glPopMatrix();
		}
	}
	
}

void MissileBattery::move(){

	if(isActive)
		if(isExploding){
			explode();
		}
		else{
			//get TM of anchor, apply transformation defined in OM, and save as this object's TM
			glMatrixMode(GL_MODELVIEW);
			glPushMatrix();
			GLfloat m[16];
			anchor->getTM(m);
			glLoadMatrixf(m);
			glMultMatrixf(OM);	
			glGetFloatv(GL_MODELVIEW_MATRIX, TM);
			glPopMatrix();
		}


	if (currentTorpedo != NULL){
		if (!currentTorpedo->active())		
			currentTorpedo = NULL;
	}
}

void MissileBattery::fireTorpedo(){

	if (isActive && torpedoCount >= 0){		
		if(currentTorpedo == NULL){			
			glMatrixMode(GL_MODELVIEW);
			glPushMatrix();
			glLoadMatrixf(TM);
			glTranslatef(0, 0, 100);
			GLfloat m[16];
			glGetFloatv(GL_MODELVIEW_MATRIX, m);
			glPopMatrix();

			currentTorpedo = new Missile(target, missileLifeSpan, m);
			torpedos.push_back(currentTorpedo);			
			torpedoCount -= 1;			
		}		
	}
}


#endif